package com.wonders.frame.autoGeneration.support.enums;

import com.wonders.frame.common.util.SpringContextUtil;
import com.wonders.frame.service.HuiAreasService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.Objects;

public enum DatasourceOperatorEnum {

    INSTANCE;


    private DatasourceTypeEnum operator;
    DatasourceOperatorEnum(){
        initOperator();
    }
    private void initOperator(){
        JdbcTemplate jt = (JdbcTemplate) SpringContextUtil.getBeanByClass(JdbcTemplate.class);
        String database = "";
        try {
            database = jt.getDataSource().getConnection().getMetaData().getDatabaseProductName();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        this.operator = DatasourceTypeEnum.MYSQL;
        for (int i = 0; i < DatasourceTypeEnum.values().length; i++) {
            if(Objects.equals(DatasourceTypeEnum.values()[i].getDatabaseType(), database)){
                this.operator = DatasourceTypeEnum.values()[i];
                System.out.println("database: " + database);
            }
        }

    }

    public static DatasourceTypeEnum getOperator(){
        return INSTANCE.operator;
    }
}
